<?php
/**
 * Stub for generic user implementation
 *
 * @package com.clear-health.clearhealth
 * @author  Joshua Eichorn <jeichorn@mail.com>
 */

$loader->requireOnce('ordo/Base_User.class.php');
$loader->requireOnce('includes/Utils.class.php');

/**
 * Stub user class
 *
 * @package com.clear-health.clearhealth
 */
class User extends Base_User {
      
   var $_internalName='User';
   var $_key = 'user_id';
   
   const ROLE_DOCTOR_ID = 12;
   const ROLE_BILLING_ID = 8;

   function User() {
      parent::Base_User();
   }

   function users_factory($group="") {
                
		$users = array();

		if (!empty($group)) {
			//op-en-hcs way of doing it
			//  $sql .= " LEFT JOIN ".$this->_prefix."users_groups as ug on ug.user_id=u.user_id LEFT JOIN ".$this->_prefix."groups as g on g.id = ug.group_id where g.name =" . $this->_db->qstr($group);
			if ($group =="provider") {
				$sql = '
					SELECT
						distinct(u.user_id )
					FROM
						user AS u
						INNER JOIN person_type AS pt USING(person_id)
						INNER JOIN enumeration_value AS ev ON(ev.key = pt.person_type)
						INNER JOIN enumeration_definition AS ed USING(enumeration_id)
					WHERE
						ed.name = "person_type" AND
						ev.value = "Provider"';
			}
		}
		else {
			$u = new User(null,null);
			$sql = 'SELECT u.user_id FROM ' . $u->tableName() . ' AS u ';
		}
		
		$sql .= " order by u.username";

		$results = $this->_db->Execute($sql) or die ("Database Error: " . $this->_db->ErrorMsg());

		while ($results && !$results->EOF) {
		$tu = new User("","");
		$tu->set("user_id",$results->fields['user_id']);
		$tu->populate();
		$users[] = $tu;
		$results->MoveNext();
		}
		return $users;
   }
	
	function getUserID($username){
		$sql = "SELECT user_id FROM user WHERE username = '$username'";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
		if (empty($result->fields)) return "not exist";
		return $result->fields['user_id'];
	}
	
	function getUserName($id){
		$sql = "SELECT username FROM user WHERE user_id = '$id'";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
		return $result->fields['username'];
	}
	
	function getAllUsernames($string = ''){
		$sql = "SELECT username FROM user";
		if ($string != '') {
			$sql .= " WHERE username LIKE '$string%'";
		}
		$rows = $this->_db->GetAll($sql);
		return $rows;
	}
	
	function getPersonName($userId){
		$sql = "SELECT employee.name
		          FROM user
		              INNER JOIN employee ON (user.person_id = employee.person_id) WHERE user_id = '$userId'";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
		return $result->fields['name'];
	}
	
    public function getPersonId($userId){
        $sql = "SELECT person_id FROM user WHERE user_id = '$userId'";
        $result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
        return $result->fields['person_id'];
    }
	
	public function getRole($userName) {
		$sql = "SELECT AroGroup.value
			FROM gacl_aro_groups AS AroGroup
			LEFT JOIN gacl_groups_aro_map AS AroGroupMap ON AroGroup.id = AroGroupMap.group_id
			LEFT JOIN gacl_aro AS Aro ON AroGroupMap.aro_id = Aro.id
			WHERE Aro.value = '$userName'";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());;
		return $result->fields['value'];
	}
	
	public function getTitleByUid($userId) {
		$field	=	L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		$sql = "SELECT employee_type.$field as name
		        FROM user
		            LEFT JOIN employee ON user.person_id = employee.person_id
				    LEFT JOIN employee_type ON employee.employee_type_id = employee_type.employee_type_id
				 WHERE user.user_id = $userId";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());;
		return $result->fields['name'];
	}
	
	public function getRoomByUid($userId) {
		$field    =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		$sql = "SELECT room.$field as name
		          FROM user
		              LEFT JOIN room ON user.room_id = room.room_id
				WHERE user.user_id = $userId";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
		return $result->fields['name'];
	}
	
	public function getPhotoUrl($userId) {
		$sql = "SELECT photo_url FROM user WHERE user_id = '$userId'";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
		return Utils::getAppUrl() . "/user/user_photo/" . $result->fields['photo_url'];
	}
	
	public function getSignatureUrl($userId){
		$sql = "SELECT signature_url FROM user WHERE user_id = '$userId'";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
		return Utils::getAppUrl() . "/user/user_signature/" . $result->fields['signature_url'];
	}
	
	public function getSignatureUrlByPersonId($personId){
		$sql = "SELECT signature_url FROM user WHERE person_id = '$personId'";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
		return Utils::getAppUrl() . "/user/user_signature/" . $result->fields['signature_url'];
	}
	
	public function checkRole($userName, $roleId) {
		$sql = "SELECT AroGroup.id
			FROM gacl_aro_groups AS AroGroup
				LEFT JOIN gacl_groups_aro_map AS AroGroupMap ON AroGroup.id = AroGroupMap.group_id
				LEFT JOIN gacl_aro AS Aro ON AroGroupMap.aro_id = Aro.id
			WHERE Aro.value = '$userName'";
		$result = $this->_db->Execute($sql) or die("Database Error: " . $this->_db->ErrorMsg());
		if ( isset($result->fields['id']) && $result->fields['id'] == $roleId ) {
			return true;
		}
		
		return false;
	}
	
}
?>
